﻿using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using Furion.DatabaseAccessor;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using Nest;
using NewLife.Xml;
using Magicodes.ExporterAndImporter.Core.Extension;
using System.IO;
namespace Admin.NET.Application;
/// <summary>
/// 学生服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class StudentService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Student> _rep;
    private readonly SqlSugarRepository<Banji> _banjiRep;
    private readonly SqlSugarRepository<JiaoxueBan> _jiaoxueBanRep;
    private readonly SqlSugarRepository<SysFile> _fileRep;
    private readonly SqlSugarRepository<Student> _studentRep;
    private readonly SqlSugarRepository<SysUser> _sysUserRep;
    private readonly SqlSugarRepository<SysUserRole> _sysUserRoleRep;
    private readonly SqlSugarRepository<SysRole> _sysRoleRep;

    public StudentService(SqlSugarRepository<Student> rep, SqlSugarRepository<SysFile> fileRep, SqlSugarRepository<Banji> banjiRep, SqlSugarRepository<JiaoxueBan> jiaoxueBanRep, SqlSugarRepository<Student> studentRep, SqlSugarRepository<SysUser> sysUserRep, SqlSugarRepository<SysUserRole> sysUserRoleRep, SqlSugarRepository<SysRole> sysRoleRep)
    {
        _rep = rep;
        _fileRep = fileRep;
        _banjiRep = banjiRep;
        _jiaoxueBanRep = jiaoxueBanRep;
        _studentRep = studentRep;
        _sysUserRep = sysUserRep;
        _sysUserRoleRep = sysUserRoleRep;
        _sysRoleRep = sysRoleRep;
    }

    /// <summary>
    /// 分页查询学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<StudentOutput>> Page(StudentInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.Xuhao.Contains(input.SearchKey.Trim())
                || u.Xh.Contains(input.SearchKey.Trim())
                || u.Name.Contains(input.SearchKey.Trim())
                || u.Sex.Contains(input.SearchKey.Trim())
                || u.IdCardNum.Contains(input.SearchKey.Trim())
                || u.NationalNum.Contains(input.SearchKey.Trim())
                || u.AssistNum.Contains(input.SearchKey.Trim())
                || u.Tel.Contains(input.SearchKey.Trim())
                || u.Beizhu0.Contains(input.SearchKey.Trim())
                || u.Beizhu1.Contains(input.SearchKey.Trim())
                || u.Beizhu2.Contains(input.SearchKey.Trim())
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.Xuhao), u => u.Xuhao.Contains(input.Xuhao.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Xh), u => u.Xh.Contains(input.Xh.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Sex), u => u.Sex.Contains(input.Sex.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.IDCardNum), u => u.IdCardNum.Contains(input.IDCardNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.NationalNum), u => u.NationalNum.Contains(input.NationalNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.AssistNum), u => u.AssistNum.Contains(input.AssistNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Tel), u => u.Tel.Contains(input.Tel.Trim()))
            .WhereIF(input.BanjiId>0, u => u.BanjiId == input.BanjiId)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Beizhu0), u => u.Beizhu0.Contains(input.Beizhu0.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Beizhu1), u => u.Beizhu1.Contains(input.Beizhu1.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Beizhu2), u => u.Beizhu2.Contains(input.Beizhu2.Trim()))
            .WhereIF(input.BeAtBook.HasValue, u => u.BeAtBook == input.BeAtBook)
            .WhereIF(input.BeAtSchool.HasValue, u => u.BeAtSchool == input.BeAtSchool)
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<Banji>((u, banjiid) => u.BanjiId == banjiid.Id )
            .LeftJoin<JiaoxueBan>((u, banjiid,jiaoxueBanid) => u.JiaoxueBanId == jiaoxueBanid.Id)
            .WhereIF(input.Ji > 0, (u, banjiid) => banjiid.Ji == input.Ji)
            .WhereIF(input.JiaoxueBanId > 0, (u, banjiid, jiaoxueBanid) => u.JiaoxueBanId == input.JiaoxueBanId)
            .Select((u, banjiid, jiaoxueBanid) => new StudentOutput
            {
                Id = u.Id,
                Xuhao = u.Xuhao,
                Xh = u.Xh,
                Name = u.Name,
                Sex = u.Sex,
                Birthday = u.Birthday,
                IdCardNum = u.IdCardNum,
                NationalNum = u.NationalNum,
                AssistNum = u.AssistNum,
                BeAtSchool = u.BeAtSchool,
                BeAtBook = u.BeAtBook,
                IsTuoguan = u.IsTuoguan,
                Tel = u.Tel,
                MotherName = u.MotherName,
                MotherTel = u.MotherTel,
                FatherName = u.FatherName,
                FatherTel = u.FatherTel,
                BanjiId = u.BanjiId, 
                BanjiIdName = banjiid.Name,
                Beizhu0 = u.Beizhu0,
                Beizhu1 = u.Beizhu1,
                Beizhu2 = u.Beizhu2,
                CreateUserName = u.CreateUserName,
                UpdateUserName = u.UpdateUserName,
                JiaoxueBanIdName = jiaoxueBanid.Name,
            });
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 分页查询学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "PageJiaoxueBan")]
    public async Task<SqlSugarPagedList<StudentOutput>> PageJiaoxueBan(StudentInput input)
    {
        var query = _rep.AsQueryable()
            .Where(u => u.BeAtSchool == true)
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.Xuhao.Contains(input.SearchKey.Trim())
                || u.Xh.Contains(input.SearchKey.Trim())
                || u.Name.Contains(input.SearchKey.Trim())
                || u.Sex.Contains(input.SearchKey.Trim())
                || u.IdCardNum.Contains(input.SearchKey.Trim())
                || u.NationalNum.Contains(input.SearchKey.Trim())
                || u.AssistNum.Contains(input.SearchKey.Trim())
                || u.Tel.Contains(input.SearchKey.Trim())
                || u.Beizhu0.Contains(input.SearchKey.Trim())
                || u.Beizhu1.Contains(input.SearchKey.Trim())
                || u.Beizhu2.Contains(input.SearchKey.Trim())
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.Xuhao), u => u.Xuhao.Contains(input.Xuhao.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Xh), u => u.Xh.Contains(input.Xh.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Sex), u => u.Sex.Contains(input.Sex.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.IDCardNum), u => u.IdCardNum.Contains(input.IDCardNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.NationalNum), u => u.NationalNum.Contains(input.NationalNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.AssistNum), u => u.AssistNum.Contains(input.AssistNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Tel), u => u.Tel.Contains(input.Tel.Trim()))
            .WhereIF(input.BanjiId > 0, u => u.BanjiId == input.BanjiId)
            .WhereIF(input.JiaoxueBanId > 0, u => u.JiaoxueBanId == input.JiaoxueBanId)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Beizhu0), u => u.Beizhu0.Contains(input.Beizhu0.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Beizhu1), u => u.Beizhu1.Contains(input.Beizhu1.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Beizhu2), u => u.Beizhu2.Contains(input.Beizhu2.Trim()))
            .WhereIF(input.BeAtBook.HasValue, u => u.BeAtBook == input.BeAtBook)
            .WhereIF(input.BeAtSchool.HasValue, u => u.BeAtSchool == input.BeAtSchool)
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<Banji>((u, banjiid) => u.BanjiId == banjiid.Id)//JiaoxueBanId
            .LeftJoin<JiaoxueBan>((u,banjiid,jiaoxueBanid) => u.JiaoxueBanId == jiaoxueBanid.Id)//JiaoxueBanId
            .WhereIF(input.Ji > 0, (u, banjiid, jiaoxueBanid) => jiaoxueBanid.Ji == input.Ji)
            .Select((u, banjiid, jiaoxueBanid) => new StudentOutput
            {
                Id = u.Id,
                Xuhao = u.Xuhao,
                Xh = u.Xh,
                Name = u.Name,
                Sex = u.Sex,
                IdCardNum = u.IdCardNum,
                NationalNum = u.NationalNum,
                AssistNum = u.AssistNum,
                BeAtSchool = u.BeAtSchool,
                BeAtBook = u.BeAtBook,
                Tel = u.Tel,
                MotherName = u.MotherName,
                MotherTel = u.MotherTel,
                FatherName = u.FatherName,
                FatherTel = u.FatherTel,
                BanjiId = u.BanjiId,
                BanjiIdName = banjiid.Name,
                Beizhu0 = u.Beizhu0,
                Beizhu1 = u.Beizhu1,
                Beizhu2 = u.Beizhu2,
                CreateUserName = u.CreateUserName,
                UpdateUserName = u.UpdateUserName,
                JiaoxueBanIdName = jiaoxueBanid.Name,
            });
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task<long> Add(AddStudentInput input)
    {
        var entity = input.Adapt<Student>();
        await _rep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteStudentInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        //await _rep.FakeDeleteAsync(entity);   //假删除
        await _rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateStudentInput input)
    {
        var entity = input.Adapt<Student>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取学生
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<Student> Detail([FromQuery] QueryByIdStudentInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取学生列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<StudentOutput>> List([FromQuery] StudentInput input)
    {
        return await _rep.AsQueryable().Select<StudentOutput>().ToListAsync();
    }

    /// <summary>
    /// 获取班级列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "BanjiBanjiIdDropdown"), HttpGet]
    public async Task<dynamic> BanjiBanjiIdDropdown()
    {
        return await _rep.Context.Queryable<Banji>()
                .Select(u => new
                {
                    Label = u.Name,
                    Value = u.Id
                }
                ).ToListAsync();
    }

    /// <summary>
    /// 获取班级列表
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "JiaoxueBanBanjiIdDropdown"), HttpGet]
    public async Task<dynamic> JiaoxueBanBanjiIdDropdown()
    {
        return await _rep.Context.Queryable<JiaoxueBan>()
                .Select(u => new
                {
                    Label = u.Name,
                    Value = u.Id
                }
                ).ToListAsync();
    }
    
    /// <summary>
    /// 导入学生
    /// </summary>
    /// <param name="fileId">文件Id</param>
    /// <returns></returns>
    [HttpPost]
    [UnitOfWork]
    [ApiDescriptionSettings(Name = "Import")]
    public async Task<string> Import(BaseIdInput fileId)
    {
        var file = _fileRep.GetById(fileId.Id);
        var filePath = App.WebHostEnvironment.WebRootPath;
        filePath = System.IO.Path.Combine(filePath, file.FilePath);
        filePath = System.IO.Path.Combine(filePath, file.Id.ToString());
        filePath += file.Suffix;

        IImporter importer = new ExcelImporter();
        var res = await importer.Import<StudentImportDto>(filePath);
        foreach (var item in res.Data)
        {
            //var student = item.Adapt<Student>();
            var student = await _rep.AsQueryable().Where(p => p.Xh == item.Xh).FirstAsync();
            if (student == null)
            {
                student = new Student();
            }

            if(item.BeAtBook.HasValue)
                student.BeAtBook = item.BeAtBook.Value;
            if(item.BeAtSchool.HasValue)
                student.BeAtSchool = item.BeAtSchool.Value;
            if (item.IsTuoguan.HasValue)
                student.IsTuoguan = item.IsTuoguan.Value;

            //学号	姓名	性别	班级	教学班	身份证号	全国学籍号	学籍辅号	手机	在校	在籍	备注0	备注1	备注2
            if (item.Xh.IsNullOrWhiteSpace() == false) student.Xh = item.Xh;
            if (item.Name.IsNullOrWhiteSpace() == false) student.Name = item.Name;
            if (item.Sex.IsNullOrWhiteSpace() == false) student.Sex = item.Sex;
            if (item.Birthday.HasValue == true)  student.Birthday = item.Birthday;
            if (item.IdCardNum.IsNullOrWhiteSpace() == false) student.IdCardNum = item.IdCardNum;
            if (item.NationalNum.IsNullOrWhiteSpace() == false) student.NationalNum = item.NationalNum;
            if (item.AssistNum.IsNullOrWhiteSpace() == false) student.AssistNum = item.AssistNum;
            if (item.Tel.IsNullOrWhiteSpace() == false) student.Tel = item.Tel;
            if (item.MotherName.IsNullOrWhiteSpace() == false) student.MotherName = item.MotherName;
            if (item.MotherTel.IsNullOrWhiteSpace() == false) student.MotherTel = item.MotherTel;
            if (item.FatherName.IsNullOrWhiteSpace() == false) student.FatherName = item.FatherName;
            if (item.FatherTel.IsNullOrWhiteSpace() == false) student.FatherTel = item.FatherTel;
            if (item.Beizhu0.IsNullOrWhiteSpace() == false) student.Beizhu0 = item.Beizhu0;
            if (item.Beizhu1.IsNullOrWhiteSpace() == false) student.Beizhu1 = item.Beizhu1;
            if (item.Beizhu2.IsNullOrWhiteSpace() == false) student.Beizhu2 = item.Beizhu2;

            if (item.BanjiIdName.IsNullOrWhiteSpace() == false)
            {
                var banji = await _banjiRep.AsQueryable().FirstAsync(p => p.Name == item.BanjiIdName) ?? throw Oops.Oh($"班级{item.BanjiIdName}不存在");
                student.BanjiId = banji.Id;
            }

            if (item.JiaoxueBanIdName.IsNullOrWhiteSpace() == false)
            {
                var jiaoxueBanji = await _jiaoxueBanRep.AsQueryable().FirstAsync(p => p.Name == item.JiaoxueBanIdName) ?? throw Oops.Oh($"班级{item.BanjiIdName}不存在");
                if (jiaoxueBanji != null)
                    student.JiaoxueBanId = jiaoxueBanji.Id;
            }

            if(student.Id != 0)
                await _rep.UpdateAsync(student);
            else
                await _rep.InsertAsync(student);
        }
        return string.Format("成功导入{0}条记录",res.Data.Count);
    }

    /// <summary>
    /// 初始化学生账号
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "InitStudentUser")]
    public async Task InitStudentUser(List<long> input)
    {
        var role = await _sysRoleRep.AsQueryable().Where(p=>p.Name == "学生").FirstAsync();
        foreach (long id in input)
        {
            try
            {
                var student = await _studentRep.GetByIdAsync(id);

                var password = student.IdCardNum;
                if (student.IdCardNum.Length == 18)
                {
                    password = student.IdCardNum.Substring(12);
                }
                var encryptPassword = CryptogramUtil.Encrypt(password);

                var user = await _sysUserRep.GetByIdAsync(student.Id);
                bool isNew = false;
                if(user == null)
                {
                    user = new SysUser { Id = 1300000000101, Account = "superadmin", Password = encryptPassword, NickName = "学生", RealName = "学生", Phone = "18020030720", Birthday = DateTime.Parse("2000-01-01"), Sex = GenderEnum.Male, AccountType = AccountTypeEnum.Member, Remark = "学生", CreateTime = DateTime.Parse("2022-02-10 00:00:00"), TenantId = 1300000000001 };
                    user.Id = student.Id;
                    user.Account = student.Xh;
                    user.AccountType = AccountTypeEnum.Member;
                    isNew = true;
                }
                user.NickName = student.Name;
                user.RealName = student.Name;
                user.Password = encryptPassword;
                user.Phone = student.Tel;
                if(isNew)
                    await _sysUserRep.InsertAsync(user);
                else
                    await _sysUserRep.UpdateAsync(user);

                var userRole = await _sysUserRoleRep.AsQueryable().Where(p=>p.UserId == user.Id && p.RoleId == role.Id).FirstAsync();
                if(userRole == null)
                {
                    await _sysUserRoleRep.InsertAsync(new SysUserRole { UserId = user.Id,RoleId = role.Id });
                }
                //_sysUserRoleRep
            }
            catch (Exception ex)
            {
            }
            
        }
    }


    /// <summary>
    /// 导出学生
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Export"), NonUnify]
    [DisplayName("导出")]
    public async Task<IActionResult> Export(StudentInput input)
    {
        var list = await _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.Xuhao.Contains(input.SearchKey.Trim())
                || u.Xh.Contains(input.SearchKey.Trim())
                || u.Name.Contains(input.SearchKey.Trim())
                || u.Sex.Contains(input.SearchKey.Trim())
                || u.IdCardNum.Contains(input.SearchKey.Trim())
                || u.NationalNum.Contains(input.SearchKey.Trim())
                || u.AssistNum.Contains(input.SearchKey.Trim())
                || u.Tel.Contains(input.SearchKey.Trim())
                || u.Beizhu0.Contains(input.SearchKey.Trim())
                || u.Beizhu1.Contains(input.SearchKey.Trim())
                || u.Beizhu2.Contains(input.SearchKey.Trim())
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.Xuhao), u => u.Xuhao.Contains(input.Xuhao.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Xh), u => u.Xh.Contains(input.Xh.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Sex), u => u.Sex.Contains(input.Sex.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.IDCardNum), u => u.IdCardNum.Contains(input.IDCardNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.NationalNum), u => u.NationalNum.Contains(input.NationalNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.AssistNum), u => u.AssistNum.Contains(input.AssistNum.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Tel), u => u.Tel.Contains(input.Tel.Trim()))
            .WhereIF(input.BanjiId > 0, u => u.BanjiId == input.BanjiId)
            .WhereIF(!string.IsNullOrWhiteSpace(input.Beizhu0), u => u.Beizhu0.Contains(input.Beizhu0.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Beizhu1), u => u.Beizhu1.Contains(input.Beizhu1.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Beizhu2), u => u.Beizhu2.Contains(input.Beizhu2.Trim()))
            .WhereIF(input.BeAtBook.HasValue, u => u.BeAtBook == input.BeAtBook)
            .WhereIF(input.BeAtSchool.HasValue, u => u.BeAtSchool == input.BeAtSchool)
            //处理外键和TreeSelector相关字段的连接
            .LeftJoin<Banji>((u, banjiid) => u.BanjiId == banjiid.Id)
            .LeftJoin<JiaoxueBan>((u, banjiid, jiaoxueBanid) => u.JiaoxueBanId == jiaoxueBanid.Id)
            .WhereIF(input.Ji > 0, (u, banjiid) => banjiid.Ji == input.Ji)
            .WhereIF(input.JiaoxueBanId > 0, (u, banjiid, jiaoxueBanid) => u.JiaoxueBanId == input.JiaoxueBanId)
            .OrderBy(u => u.Xh)
            .Select((u, banjiid, jiaoxueBanid) => new ExportStudentDto
            {
                Xh = u.Xh,
                Name = u.Name,
                Sex = u.Sex,
                Birthday = u.Birthday,
                IdCardNum = u.IdCardNum,
                NationalNum = u.NationalNum,
                AssistNum = u.AssistNum,
                BeAtSchool = u.BeAtSchool,
                BeAtBook = u.BeAtBook,
                Tel = u.Tel,
                MotherName = u.MotherName,
                MotherTel = u.MotherTel,
                FatherName = u.FatherName,
                FatherTel = u.FatherTel,
                BanjiIdName = banjiid.Name,
                Beizhu0 = u.Beizhu0,
                Beizhu1 = u.Beizhu1,
                Beizhu2 = u.Beizhu2,
                JiaoxueBanIdName = jiaoxueBanid.Name,
                IsTuoguan = u.IsTuoguan,
            }).ToListAsync();

        //var list = await query.ToListAsync();

        IExcelExporter excelExporter = new ExcelExporter();
        var res = await excelExporter.ExportAsByteArray(list);
        return new FileStreamResult(new MemoryStream(res), "application/octet-stream") { FileDownloadName = DateTime.Now.ToString("yyyyMMddHHmm") + "学生.xlsx" };
    }
}

